﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Mimoza.AgentLayer;

namespace SystemUpdate
{   
    public partial class Step3 : UserControl
    {
        public delegate void UpdateProgressDelegate(Status step, int progress, string message);
        
        public void UpdateProgress(Status step,int progress, string message)
        {
            if (this.progressBar1.InvokeRequired)
            {
                UpdateProgressDelegate updateProgressDel = new UpdateProgressDelegate(UpdateProgress);
                this.Invoke(updateProgressDel, new object[] { step, progress, message });
            }
            else
            {
                this.progressBar1.Value = progress;
                switch (step)
                {
                    case Status.Progress: 
                        this.download.Text = progress.ToString() + "%"; 
                        break;
                    case Status.WrongPlatform:
                        MessageBox.Show(Resources.Error_WrongPlatform, Resources.Error_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        (Program.mainForm as SystemUpdateForm).Close();

                        break;
                    case Status.Error:
                        MessageBox.Show(Resources.Error_Critical + message, Resources.Error_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        (Program.mainForm as SystemUpdateForm).Close();

                        break;
                    case Status.Finish: 
                        //MessageBox.Show(message, "Sysetm Update", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                        //(Program.mainForm as SystemUpdateForm).Close();

                        if (DialogResult.OK == MessageBox.Show(Resources.Reboot_Message, Resources.Reboot_Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                        {
                            SystemPower.Reboot();
                        }
                        (Program.mainForm as SystemUpdateForm).Close();
                        break;
                }                
            }
        }

        public Step3()
        {
            InitializeComponent();
            (Program.mainForm as SystemUpdateForm).downloadFrom.OnProgressChangedEvent += this.OnProgressChangedEvent;
            
            this.download.Text = "";
            //this.update.Text = "";
        }

        public void OnProgressChangedEvent(object sender, ProgressEventArgs e)
        {
            UpdateProgress(e.Step,e.Progress, e.Message);
        }
    }
}
